#ifndef TRANSFERDAEMON_H
#define TRANSFERDAEMON_H

#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

#include "ImageTransferProtocol.h"

typedef void requestHandler(normal_request_t request, struct sockaddr_in addr, socklen_t addr_len);
class TransferDaemon
{
public:
    TransferDaemon();
    ~TransferDaemon();
    void startAnswer();
    void sendData(void* data, int size, struct sockaddr_in addr, socklen_t addr_len);
    void setRequestHandler(requestHandler* handler){
        this->m_request_handler = handler;
    }
    friend void* ansThread(void* _tran);
private:
    void answer();
    int establishSocket(int port = DEFAULT_SERVER_PORT);
    void dealWithRequest(normal_request_t request, struct sockaddr_in addr, socklen_t addr_len);
    requestHandler* m_request_handler;

    int m_port;
    int m_sock;
    bool m_working;
    pthread_t m_ans_thread;
};

#endif // TRANSFERDAEMON_H
